#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import json
import sys

from utils.DateUtil import DateUtil

class ConfTv() :

    def __init__(self, logger, mqttClient, sn) :
        self.logger = logger
        self.mqttClient = mqttClient
        self.sn = sn

    def conf(self, message):
        # 校验重启目标是否是本机
        if message["sn"] != self.sn :
            data = {"sn": self.sn, "status": "fasle", "code": "TARGET_ERROR", "msg": "目标序列号错误", "uploadTime": DateUtil.getNowDate()}
            self.mqttClient.publish("CONFIG_TV_CALLBACK", json.dumps(data, ensure_ascii=False), 2)
            return
        # global CURRENT_CONFIG_TI_ID
        # congId = message["configId"]
        channelList = message["channelList"]
        file = "..\channel.html"
        with open(sys.path[0] + file, 'w') as f:
            f.write(str(channelList))
        # configId 标记为最新
        # CURRENT_CONFIG_TI_ID = congId
        data = {"sn": self.sn, "status": "true", "code": "SUCCESS", "msg": "频道配置成功", "uploadTime": DateUtil.getNowDate()}

        self.mqttClient.publish("CONFIG_TV_CALLBACK", json.dumps(data, ensure_ascii=False), 2)
        # if congId != CURRENT_CONFIG_TI_ID :
        #     channelList = message["channelList"]
        #     file = "\channel.html"
        #     with open(sys.path[0] + file, 'w') as f:
        #         f.write(str(channelList))
        #     # configId 标记为最新
        #     CURRENT_CONFIG_TI_ID = congId
        #     data = {"sn": self.sn, "status": "true", "code": "SUCCESS", "msg": "频道配置成功", "uploadTime": DateUtil.getNowDate()}
        #     self.publish("CONFIG_TV_CALLBACK", data)
        # else :
        #     data = {"sn": self.sn, "status": "true", "code": "SUCCESS", "msg": "频道配置成功", "uploadTime": DateUtil.getNowDate()}
        #     self.publish("CONFIG_TV_CALLBACK", data)
